(function(){
'use strict';

angular.module('Contacts.infos')

        .controller('InfosController', function ($stateParams, $http, $rootScope, $scope, ngDialog) {

            var infos = this;           
            this.getInfos = function (userId, PageNum) {

                if (PageNum == null || PageNum < 1)
                	PageNum = 1;

                $http({
                    url: 'info/' + userId,
                    method: "GET",
                    params: {
                        "page": PageNum
                    }
                }).success(function (data) {
                    $rootScope.infoPage = data;
                    if (data.numberOfElements >= 15){
                    	$rootScope.disableAddInfo = true;
                    }
                    else{
                    	$rootScope.disableAddInfo = false;
                    }
                    infos.currentUser = userId;
                    $rootScope.currentUser = userId;
                    $rootScope.hideInfo = false;
                });
            };
            
            this.getInfoNames = function () {

                $http({
                    url: 'info/names',
                    method: "GET"
                }).success(function (data) {
                    $rootScope.infoNames = data;
                });
            };
            
            this.openDeleteInfoDialog = function (infoId) {
            	
            	$scope.infoId = infoId;
            	
                ngDialog.close();
                ngDialog.open({
					template: 'app/infos/dialogDeleteInfo.html',
					scope: $scope
                });
            };
            
            this.openEditInfoDialog = function (info) {
            	
            	$scope.infoToModify = angular.copy(info);
            	if (info.infoName.decimal){
                	$rootScope.pat = /^[0-9+() -]+$/;
                }
                else{
                	$rootScope.pat = "";
                }
//                ngDialog.close();
                ngDialog.open({
					template: 'app/infos/dialogEditInfo.html',
					scope: $scope
                });
            };

            this.openCreateInfoDialog = function () {
  
            	
//                ngDialog.close();
            	infos.getInfoNames();
                ngDialog.open({
					template: 'app/infos/dialogCreateInfo.html',
                });
            };
            
            this.createInfo = function (userId, infoNameId, value) {

                $http.put('info/create', {
                    "userId": userId,
                    "infoNameId": infoNameId,
                    "value": value.trim(),
                }).success(function (data) {
                    infos.getInfos($rootScope.currentUser, 1);
                }).error(function (data, status) {
                	$scope.errorMessage = data.message;
            		ngDialog.close();
                    ngDialog.open({
                        template: 'app/infos/dialogCreateFail.html',
                        scope: $scope
                    });
                });
                ngDialog.closeAll();
            };
            
            this.updateInfo = function (id, userId, infoNameId, value) {
                $http.post('info/update', {
                	"id": id,
                	"userId": userId,
                	"infoNameId": infoNameId,
                    "value": value.trim(),
                }).success(function (data, status) {
                	infos.getInfos($rootScope.currentUser, 1);
                }).error(function (data, status) {
                	$scope.errorMessage = data.message;
            		ngDialog.close();
                    ngDialog.open({
                        template: 'app/infos/dialogCreateFail.html',
                        scope: $scope
                    });
                });
//                ngDialog.closeAll();
            };
            
            
            this.removeInfo = function (id) {
                $http({
                    url: 'info/delete/' + id,
                    method: "DELETE"
                }).success(function (data) {
                    infos.getInfos($rootScope.currentUser, 1);
                });
                ngDialog.close();
            };
            
            this.closeAll = function () {
                ngDialog.closeAll();
            };
            
            this.setInfoName = function (infoName) {
                $rootScope.infoN = infoName;
                if (infoName.decimal){
                	$rootScope.pat = /^[0-9+() -]+$/;
                }
                else{
                	$rootScope.pat = "";
                }
            };
            

          
        });

})();